package com;

import java.io.*;
import java.util.*;

/**
 * @author Wes Billman
 * Created: Nov 28, 2006
 */
public class InstalledAddons
{
    public enum ERROR_CODE
    {
        ERROR_NONE,
        ERROR_WOW_INSTALL_DIR_NOT_FOUND,
        ERROR_ADDONS_FOLDER_NOT_FOUND,
        ERROR_NO_WOWACE_ADDONS_FOUND
    }
    private final String ACE_ADDON_CHANGELOG_PATTERN = "changelog-r";
    private final boolean bDebug = false;
    
    private ArrayList<WowAceAddon> addons;
    
    public InstalledAddons()
    {
        addons = new ArrayList<WowAceAddon>();
    }
    
    public ERROR_CODE reloadInstalledAddons(String wowInstallPath)
    {
        //First make sure wow installation path is correct.
        File wowInstallDir = new File(wowInstallPath);
        if(!wowInstallDir.exists())
        {
            return ERROR_CODE.ERROR_WOW_INSTALL_DIR_NOT_FOUND;
        }
        
        //Then check for addons folder
        File addonsDir = new File(wowInstallPath);
        if(!addonsDir.exists())
        {
            return ERROR_CODE.ERROR_ADDONS_FOLDER_NOT_FOUND;
        }
        
        //Get the folders in the addons directory
        File[] addonFolders = addonsDir.listFiles();
        WowAceAddon aceAddon;
        
        //	Clear any leftovers in the addons list.
        addons.clear();
        
        //	Search through the addons directory to find installed addons.
        for(int i = 0; i < addonFolders.length; i++)
        {
            if(isWowAceAddon(addonFolders[i]))
            {
                aceAddon = new WowAceAddon(	getAddonName(addonFolders[i]),
                        getAddonDescription(addonFolders[i]),
                        "",
                        getAddonVersion(addonFolders[i]));
                addons.add(aceAddon);
            }
        }
        
        if(addons.size() <= 0)
        {
            return ERROR_CODE.ERROR_NO_WOWACE_ADDONS_FOUND;
        }
        
        return ERROR_CODE.ERROR_NONE;
    }
    
    public boolean isWowAceAddon(File addon)
    {
        File files[] = addon.listFiles();
        
        if(!addon.isDirectory())
        {
            if(bDebug)
            {
                System.out.println(addon.getName() + " is Not a directory");
            }
            return false;
        }
        
        for(int i = 0; i < files.length; i++)
        {
            if(files[i].getName().toLowerCase().contains(ACE_ADDON_CHANGELOG_PATTERN))
            {
                System.out.println("Found Addon Directory: " + addon.getName());
                return true;
            }
        }
        
        return false;
    }
    
    private String getAddonName(File addon)
    {
        String addonName = null;
        
        addonName = addon.getName();
        
        System.out.println("Name: " + addonName);
        return addonName;
    }
    
    private String getAddonDescription(File addon)
    {
        String addonDesc = null;
        
        return addonDesc;
    }
    
    private String getAddonVersion(File addon)
    {
        String addonVersion = null;
        int maxVersion = 0;
        File files[] = addon.listFiles();
        
        for(int i = 0; i < files.length; i++)
        {
            if(files[i].getName().toLowerCase().contains(ACE_ADDON_CHANGELOG_PATTERN))
            {
                addonVersion = files[i].getName();
                addonVersion = addonVersion.replace(ACE_ADDON_CHANGELOG_PATTERN, "");
                addonVersion = addonVersion.replace(".txt", "");
                if(Integer.parseInt(addonVersion) > maxVersion)
                {
                    maxVersion = Integer.parseInt(addonVersion);
                }
            }
        }
        
        
        
        System.out.println("Version: " + addonVersion);
        return maxVersion + "";
    }
    
    public ArrayList<WowAceAddon> getAddons()
    {
        return addons;
    }
}
